<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('申请补卡')" />
    <th:block th:include="include :: datetimepicker-css" />

    <link th:href="@{/ajax/libs/layui/css/layui.css}" rel="stylesheet"/>
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-leave-add" th:object="${hrFillClock}">

            <input name="id" th:value="*{id}" type="hidden">

            <div class="form-group">
                <label class="col-sm-3 control-label">员工姓名：</label>
                <div class="col-sm-8">
                    <input name="applyUserName" th:value="*{applyUserName}" class="form-control" type="text" readonly>
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label"><span style="color: red; ">*</span>补卡时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        <input name="dates" class="form-control" readonly="readonly" placeholder="yyyy-MM-dd" type="text" required>
                        <input name="classDate" type="hidden" th:value="${classDate}">
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label"><span style="color: red; ">*</span>未打卡原因：</label>
                <div class="col-sm-8">
                    <span>[[${hrFillClockCount}]]</span>
                    <textarea id="reason" name="reason" class="form-control" required></textarea>
                    <span>每人每月有3次免费补卡机会，每次补卡只能申请一次。</span>
                </div>
            </div>
            <div class="form-group">
                <input id="imgUrls" name="imgUrls" type="hidden" >
                <label class="col-sm-3 control-label">图片：</label>
                <div class="col-sm-8">
                    <div class="layui-upload">
                        <button type="button" class="layui-btn layui-btn-normal" id="testList">选择多图片</button>
                        <div class="layui-upload-list">
                            <table class="layui-table">
                                <thead>
                                <tr><th>文件名</th>
                                    <th>大小</th>
                                    <!--<th>状态</th>-->
                                    <th>操作</th>
                                </tr></thead>
                                <tbody id="demoList"></tbody>
                            </table>
                        </div>
                        <button type="button" style="display: none;" class="layui-btn" id="testListAction" >开始上传</button>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />

    <script th:src="@{/ajax/libs/layui/layui.all.js}"></script>
    <script th:inline="javascript">
        var prefix = ctx + "hr/clock"
        var datas = [[${@dict.getType('biz_leave_type')}]];
        $("#form-leave-add").validate({
            focusCleanup: true
        });


        $("#reason").keyup(function(){
            var str = $("#reason").val();
            if (str.length > 50 ){
                $("#reason").val(str.substring(0,50));
                $.modal.alertWarning("字数限制在50字以内");
            }
        });


        function submitHandler() {

            var str = $("#reason").val();

            if (str.length > 50 ){
                $("#reason").val(str.substring(0,50));
                $.modal.alertWarning("字数限制在50字以内");
                return;
            }


            if ($.validate.form() ) {

                //上传图片
                $("#testListAction").click();

                if (!$("#demoList").html()){
                    //没有图片直接提交表单
                    $.operate.save(prefix + "/submitApply", $('#form-leave-add').serialize());
                }

                // $.operate.save(prefix + "/add", $('#form-leave-add').serialize());
            }
        }

        $("input[name='dates']").datetimepicker({
            format: "yyyy-mm-dd hh:ii",
            minView: 0,
            autoclose: true,
            minuteStep: 30
        });



        $(document).ready(function () {

            layui.use('upload', function(){
                var $ = layui.jquery
                    ,upload = layui.upload;

                //多文件列表示例
                var demoListView = $('#demoList')
                    , uploadListIns = upload.render({
                    elem: '#testList'
                    , url: '/common/upload' //改成您自己的上传接口
                    , accept: 'images'
                    , multiple: true
                    , number: 8
                    , acceptMime:  'image/*'
                    , auto: false
                    , bindAction: '#testListAction'
                    , choose: function (obj) {

                        var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
                        //读取本地文件
                        obj.preview(function (index, file, result) {
                            var tr = $(['<tr id="upload-' + index + '">'
                                /*, '<td>' + file.name + '</td>'*/
                                , '<td>' +'<img onclick="$.table.imageViewSimp(\''+ result +'\')" src="'+ result +'">' + '</td>'
                                , '<td>' + (file.size / 1024).toFixed(1) + 'kb</td>'
                                /*, '<td>等待上传</td>'*/
                                , '<td>'
                                , '<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
                                , '<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'
                                , '</td>'
                                , '</tr>'].join(''));

                            //单个重传
                            tr.find('.demo-reload').on('click', function () {
                                obj.upload(index, file);
                            });

                            //删除
                            tr.find('.demo-delete').on('click', function () {
                                delete files[index]; //删除对应的文件
                                tr.remove();
                                uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
                            });

                            demoListView.append(tr);
                        });
                    }
                    , done: function (res, index, upload) {
                        console.log("done:"+res); //得到总文件数
                        console.log("done:"+index); //请求成功的文件数
                        console.log("done:"+upload); //请求失败的文件数
                        if (res.code == 0) { //上传成功

                            var tr = demoListView.find('tr#upload-' + index)
                                , tds = tr.children();
                            /*tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');*/
                            tds.eq(2).html(''); //清空操作

                            $('#imgUrls').val($('#imgUrls').val() + "," + res.url);

                            return delete this.files[index]; //删除文件队列已经上传成功的文件
                        }
                        this.error(index, upload);
                    },allDone: function(obj){ //当文件全部被提交后，才触发
                        console.log("allDone:"+obj.total); //得到总文件数
                        console.log("allDone:"+obj.successful); //请求成功的文件数
                        console.log("allDone:"+obj.aborted); //请求失败的文件数

                        //提交表单
                        $.operate.save(prefix + "/submitApply", $('#form-leave-add').serialize());
                    }
                    , error: function (index, upload) {
                        var tr = demoListView.find('tr#upload-' + index)
                            , tds = tr.children();
                        /*tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');*/
                        tds.eq(2).find('.demo-reload').removeClass('layui-hide'); //显示重传
                    }
                });
            });
        });

    </script>
</body>
</html>
